package whatever;

public class T2 {

    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) {this.val = val;}

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode recordHead = head;
        int carry = 0;
        int sum = 0;



        return recordHead;
    }

    public static ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode recordHead = head;
        int carry = 0;
        int num = 0;

        while (l1 != null && l2 != null) {
            num = l1.val + l2.val + carry;
            if (num > 9) {
                carry = num / 10;
                num -= 10;
            } else {
                // carry归零
                carry = 0;
            }
            head.val = num;

            l1 = l1.next;
            l2 = l2.next;
            if (l1 != null && l2 != null)
                head.next = new ListNode();
            if (l1 != null || l2 != null) {
                head = head.next;
            }
        }

        if (l1 != null) {
            while (l1 != null) {
                num = l1.val + carry;
                if (num > 9) {
                    carry = num / 10;
                    num -= 10;
                } else {
                    // carry归零
                    carry = 0;
                }
                head.val = num;
                l1 = l1.next;
                if (l1 != null) {
                    head.next = new ListNode();
                    head = head.next;
                }
            }
        } else if (l2 != null) {
            while (l2 != null) {
                num = l2.val + carry;
                if (num > 10) {
                    carry = num / 10;
                    num -= 10;
                } else {
                    // carry归零
                    carry = 0;
                }
                head.val = num;
                l2 = l2.next;
                if (l2 != null) {
                    head.next = new ListNode();
                    head = head.next;
                }
            }
        }

        // 末结点
        if (carry != 0) {
            head.next = new ListNode();
            head = head.next;
            head.val = carry;
        } else {
            head = null;
        }
        return recordHead;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
        ListNode ll = new ListNode(9);
        ListNode lll = new ListNode(9);
        ListNode ll4 = new ListNode(9);
        ListNode l5 = new ListNode(9);
        ListNode l6 = new ListNode(9);
        ListNode l7 = new ListNode(9);
        l1.next = ll; ll.next = lll; lll.next = ll4; ll4.next = l5; l5.next = l6; l6.next = l7;

        ListNode l2 = new ListNode(9);
        ListNode l22 = new ListNode(9);
        ListNode l222 = new ListNode(9);
        ListNode l2222 = new ListNode(9);
        l2.next = l22; l22.next = l222; l222.next = l2222;

        addTwoNumbers(l1, l2);
        System.out.println(addTwoNumbers(l1, l2).toString());
    }

    public static void main2(String[] args) {
        ListNode l1 = new ListNode(0);
        ListNode ll = new ListNode(0);

        System.out.println(addTwoNumbers(l1, ll).toString());
    }

    public static void main0(String[] args) {
        ListNode l1 = new ListNode(2);
        ListNode ll = new ListNode(4);
        ListNode lll = new ListNode(3);
        l1.next = ll; ll.next = lll;

        ListNode l2 = new ListNode(5);
        ListNode l22 = new ListNode(6);
        ListNode l222 = new ListNode(4);
        l2.next = l22; l22.next = l222;

        addTwoNumbers(l1, l2);
        System.out.println(addTwoNumbers(l1, l2).toString());
    }

}
